#include "classlevel.h"
#include "specialability.h"
#include "characterclass.h"


#include <QTimer>
#include <QStringList>


ClassLevel::ClassLevel(DTwentyItem *parent) :
    SpecialAbilities(parent)
{
    qRegisterMetaType<SpecialAbilities*>();

    QTimer::singleShot(0, this, SLOT(onInitChildren()));
}

ClassLevel::ClassLevel(const ClassLevel &other) :
    SpecialAbilities(other)
{
}

bool ClassLevel::removable() const
{
    return true;
}

int ClassLevel::type() const
{
    return qMetaTypeId<ClassLevel*>();
}

int ClassLevel::level() const
{
    QList<const ClassLevel*> levels = parent()->findChildren<const ClassLevel*>(QString(), Qt::FindDirectChildrenOnly);
    return levels.indexOf(this) + 1;
}


QVariant ClassLevel::hitDieValue() const
{
    CharacterClass* cc = characterClass();
    if (!cc)
    {
        return QVariant();
    }
    int min = level();
    int max = cc->hitDie()*min;
    return QString("%1 - %2").arg(min).arg(max);
}

QVariant ClassLevel::skillPointsValue() const
{
    CharacterClass* cc = characterClass();
    if (!cc)
    {
        return QVariant();
    }

    int sp = cc->skillPoints();
    if (level() == 1)
    {
        return QString("(%1 + Int bonus) x 4").arg(sp);
    }

    return QString("%1 + Int bonus").arg(sp);
}

QVariant ClassLevel::baseAttackValue() const
{
    CharacterClass* cc = characterClass();
    if (!cc)
    {
        return QVariant();
    }

    int bonusCategory = cc->baseAttack();
    int bonus = CharacterClass::calcBaseBonus(level(), bonusCategory);

    return QString("+%1").arg(bonus);
}

QVariant ClassLevel::fortSaveValue() const
{
    CharacterClass* cc = characterClass();
    if (!cc)
    {
        return QVariant();
    }

    int bonus = CharacterClass::calcSaveBonus(level(), cc->fortSave());
    if (bonus < 0)
    {
        return tr("Invalid!");
    }

    return QString("+%1").arg(bonus);
}

QVariant ClassLevel::refSaveValue() const
{
    CharacterClass* cc = characterClass();
    if (!cc)
    {
        return QVariant();
    }
    int bonus = CharacterClass::calcSaveBonus(level(), cc->refSave());
    if (bonus < 0)
    {
        return tr("Invalid!");
    }

    return QString("+%1").arg(bonus);
}

QVariant ClassLevel::willSaveValue() const
{
    CharacterClass* cc = characterClass();
    if (!cc)
    {
        return QVariant();
    }

    int bonus = CharacterClass::calcSaveBonus(level(), cc->willSave());
    if (bonus < 0)
    {
        return tr("Invalid!");
    }

    return QString("+%1").arg(bonus);
}

QString ClassLevel::specialAbilities() const
{
    QStringList specials;
    foreach (SpecialAbility* special, findChildren<SpecialAbility*>(QString(), Qt::FindDirectChildrenOnly))
    {
        specials << special->name();
    }

    qSort(specials);
    return specials.join(", ");
}

QVariant ClassLevel::sortData() const
{
    return level();
}

void ClassLevel::onInitChildren()
{
    setName(QString("%1").arg(level()));
//    setName(QString("%1 %2 %3").arg(tr("Level")).arg(level()).arg(tr("Specials")));

    SpecialAbilities* campaignSpecials = rootItem()->findChild<SpecialAbilities*>(QString(), Qt::FindDirectChildrenOnly);

    if (!campaignSpecials)
    {
        return;

    }

    setValueSource(campaignSpecials);
}

CharacterClass *ClassLevel::characterClass() const
{
    return qobject_cast<CharacterClass*>(parent()->parent());
}

